#!/bin/bash
set -e

if [ $# -lt 3 ];then
    echo "please input obs_web_ip obs_repo_ip obs_src_ip"
    exit 2
fi
obs_ui_ip=$1
obs_repo_ip=$2
obs_src_ip=$3

OBS_INSTALL_DIR=/tmp/obs_worker_arm_install

install_rpms="ntpdate vim cpio curl perl-Compress-Zlib perl-TimeDate perl-Data-Dumper perl-XML-Parser screen psmisc bash binutils bsdtar lzma util-linux openslp lvm2 perl-Digest-MD5 osc git screen tmux wget expect"

function fn_prepare_repos()
{
    [ ! -d $OBS_INSTALL_DIR ] && mkdir -p $OBS_INSTALL_DIR
    # download obsworker scripts
    if [ ! -e $OBS_INSTALL_DIR/obsworker.tar.gz ]; then
        curl http://10.120.107.75/xdf/obsworker.tar.gz -o $OBS_INSTALL_DIR/obsworker.tar.gz
    fi
}

function fn_config_umask()
{
    sed -i s'/umask 0077/umask 0022/g' /etc/bashrc
}

function fn_config_disk()
{
    disk=$1
    if [ "x$disk" == "x" ];then
        echo "please disk name"
        exit 1
    fi

    if [ ! -e $disk ];then
        echo "No date disk $disk"
        exit 1
    fi

    if [ ! -d /var/cache/obs/worker/ ];then
        mkdir -p  /var/cache/obs/worker/
    fi

    if [ ! -e  /dev/OBS/worker ];then

        pvcreate $disk
        vgcreate "OBS" $disk
        lvcreate  -l 100%FREE  "OBS" -n "worker"
        mkfs.ext3 /dev/OBS/worker
        echo "/dev/mapper/OBS-worker /var/cache/obs/worker  ext3 defaults 0 0" >> /etc/fstab
        mount -a
    fi
}

function fn_install_obsworker()
{
    if [ ! -e /etc/sysconfig/obs-server ];then
        tar -xvf $OBS_INSTALL_DIR/obsworker.tar.gz -C /
    fi

    ## add obsrun group
    getent group obsrun >/dev/null || groupadd -r obsrun

    ## add obsrun user
    getent passwd obsrun >/dev/null || /usr/sbin/useradd -r -g obsrun -d /usr/lib/obs -s /usr/sbin/nologin -c "User for build service backend" obsrun
}

function fn_install_rpms()
{
    [ ! -d $OBS_INSTALL_DIR ] && mkdir -p $OBS_INSTALL_DIR

    repo_conf=$OBS_INSTALL_DIR/centos7.5-obs-repo.conf
    if [ -e  "$repo_conf" ];then
        rm -rf "$repo_conf"
    fi

    touch $OBS_INSTALL_DIR/centos7.5-obs-repo.conf

# notice no indent
    cat >$repo_conf << EOF
[main]
cachedir=/var/cache/yum/xxx
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=3
reposdir=/xxx

[centso7.5-aarch64]
name=centos7.5-aarch64
baseurl=http://10.175.100.158/iso/centos/7.5/CentOS-7-aarch64/
enabled=1
gpgcheck=0

[centos7.5-osc]
name=centos7.5-osc
baseurl=http://10.120.107.75/OBS:/Server:/2.9/centos_7_osc/
enabled=1
gpgcheck=0
EOF

    yum -c $repo_conf clean all
    yum -y install -c $repo_conf ${install_rpms}

}

function fn_config_hostname()
{
    n=`lscpu | grep '^CPU(s):' | awk '{print $2}'`
    DEFAULT_ROUTE_INTERFACE=`LANG=C ip route show|perl  -e '$_=<>; ( m/^default via.*dev\s+([\w]+)\s.*/ ) && print $1'`
    FQHOSTNAME=`LANG=C ip addr show $DEFAULT_ROUTE_INTERFACE| perl -lne '( m#^\s+inet\s+([0-9\.]+)(/\d+)?\s+.*# ) && print $1' | grep -v ^127. | head -n 1`
    hostname="arm${n}u${FQHOSTNAME//\./b}"
    hostnamectl set-hostname ${hostname}
}

function fn_config_obs_server_file()
{

    n=`lscpu | grep '^CPU(s):' | awk '{print $2}'`
    let n=$n/4+1
    sed -i "s/OBS_SRC_SERVER=.*/OBS_SRC_SERVER=\"$obs_src_ip:5352\"/g" /etc/sysconfig/obs-server
    sed -i "s/OBS_REPO_SERVERS=.*/OBS_REPO_SERVERS=\"$obs_repo_ip:5252\"/g" /etc/sysconfig/obs-server
    sed -i "s/OBS_WORKER_INSTANCES=.*/OBS_WORKER_INSTANCES=\"$n\"/g" /etc/sysconfig/obs-server
    sed -i "s/OBS_WORKER_JOBS=.*/OBS_WORKER_JOBS=\"8\"/g" /etc/sysconfig/obs-server

}

function fn_config_hosts()
{
    sed -i '/euleros-obs-repo/d' /etc/hosts
    sed -i '/euleros-obs/d' /etc/hosts

    sed -i "1i $obs_repo_ip euleros-obs-repo euleros-obs-repo.huawei.com"  /etc/hosts
    sed -i "1i $obs_ui_ip euleros-obs euleros-obs.huawei.com"  /etc/hosts

}

function fn_restart_obsworker()
{
    systemctl enable obsworker.service
    systemctl restart obsworker.service
}

function fn_sync_time()
{
    # sync time
    /usr/sbin/ntpdate 172.19.1.63 &> /dev/null
    /usr/sbin/ntpdate 172.19.1.64 &> /dev/null
    /usr/sbin/ntpdate 172.19.64.32 &> /dev/null

    # set utc time
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Etc/UTC /etc/localtime


    if ! grep "172.19.64.32" /var/spool/cron/root ;then
        echo "0 * * * * /usr/sbin/ntpdate 172.19.1.63 >> /var/log/szntp01.log 2>&1" >> /var/spool/cron/root
        echo "30 * * * * /usr/sbin/ntpdate 172.19.1.64 >> /var/log/szntp02.log 2>&1" >> /var/spool/cron/root
        echo "45 * * * * /usr/sbin/ntpdate 172.19.64.32 >> /var/log/nkgntp01.log 2>&1" >> /var/spool/cron/root
    fi
}
function fn_config_firewall()
{
    systemctl stop firewalld.service
    systemctl disable firewalld.service
    ## disable SELinux
    ### disable SELinux temporarily
    setenforce 0
    ###  disable SELinux permanently
    sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
}



fn_main()
{
    fn_prepare_repos
    fn_config_umask
    #fn_config_disk /dev/sdb
    fn_install_rpms
    fn_install_obsworker
    fn_sync_time
    fn_config_obs_server_file
    fn_config_hostname
    fn_config_hosts
    fn_config_firewall
    fn_restart_obsworker
}

fn_main
